<?php
namespace x\my;

class TongLianCash
{

    private $url='https://tlt.allinpay.com/aipg/ProcessServlet';

    protected $config=[
        'user'=>'20060400000044502', #通联用户名
        'pwd'=>'`12qwe', #通联密码
        'merid'=>'200604000000445', #通联商户id
        'certFile'=>__DIR__.'/TongLian/data/allinpay-pds.pem', #通联公钥
        'privateKeyFile'=>__DIR__.'/TongLian/data/20060400000044502.pem', #通联发给商户的私钥
        'certpwd'=>'111111', #打开私钥证书需要的密码
        'VERSION' => '03',
        'DATA_TYPE' => '2',
        'LEVEL' => '6',
    ];

    public function __construct($config=[])
    {
        if(is_array($config) && count($config))
        {
            $this->_config($config);
        }
        else{
            $this->url="http://127.0.0.1:8083/aipg/ProcessServlet";
            $this->config['certFile']=realpath($this->config['certFile']);
            $this->config['privateKeyFile']=realpath($this->config['privateKeyFile']);
        }

    }

    /**
     * 查询出款状态
     * @param string $orderNo
     * @return string|boolean|NULL|unknown
     */
    public function query($orderNo='',$debug=false)
    {
        $params = [
            'INFO' => [
                'TRX_CODE' => '200004',
                'VERSION' => '04',
                'DATA_TYPE' => '2',
                'LEVEL' => '6',
                'USER_NAME' => $this->config['user'],
                'USER_PASS' => $this->config['pwd'],
                'REQ_SN' => $orderNo,
            ],
            'QTRANSREQ' => [
                'QUERY_SN' => $orderNo,
                'MERCHANT_ID' => $this->config['merid'],
                'STATUS' => '2',
                'TYPE' => '1',
                'START_DAY' => '',
                'END_DAY' => ''
            ],
        ];
        //发起请求
        $tools=new PhpTools($this->config['certFile'],$this->config['privateKeyFile'],$this->config['certpwd'],$this->url);



        $r=$tools->send( $params);

        if($debug) t($r);

        if($r && isset($r["AIPG"]['QTRANSRSP']['QTDETAIL']['RET_CODE']))
        {
            $d['code']=$r["AIPG"]['QTRANSRSP']['QTDETAIL']['RET_CODE'];
            $d['msg']=$r["AIPG"]['QTRANSRSP']['QTDETAIL']['ERR_MSG'];
        }
        else
        {
            $d['code']=isset($r["AIPG"]['INFO']['RET_CODE'])?$r["AIPG"]['INFO']['RET_CODE']:1;
            $d['msg']=isset($r["AIPG"]['INFO']['ERR_MSG'])?$r["AIPG"]['INFO']['ERR_MSG']:'其它状态';
        }
        return $d;

    }

    /**
     * 出款
     * @param string $orderNo 单据号
     * @param string $name 收款人姓名
     * @param string $bankNo 收款人卡号
     * @param number $amount 出款金额
     * @param string $note 备注
     */
    public function cash($orderNo='',$name='',$bankNo='',$amount=1,$note='提现')
    {
        $params = [
            'INFO' => [
                'TRX_CODE' => '100014',
                'VERSION' => '03',
                'DATA_TYPE' => '2',
                'LEVEL' => '6',
                'USER_NAME' => $this->config['user'],
                'USER_PASS' => $this->config['pwd'],
                'REQ_SN' => $orderNo,
            ],
            'TRANS' => [
                'BUSINESS_CODE' => '09900',
                'MERCHANT_ID' => $this->config['merid'],
                'SUBMIT_TIME' => time(),
                'E_USER_CODE' => '',
                'BANK_CODE' => '',
                'ACCOUNT_TYPE' => '',
                'ACCOUNT_NO' => $bankNo,
                'ACCOUNT_NAME' => $name,
                'ACCOUNT_PROP' => '0',
                'AMOUNT' => $amount,
                'CURRENCY' => 'CNY',
                'ID_TYPE' => '0',
                'CUST_USERID' => '',
                'SUMMARY' => '',
                'REMARK' => $orderNo,
            ],
        ];


        $d=[
            'code'=>1,
            'msg'=>'',
        ];

        //发起请求
        $tools=new PhpTools($this->config['certFile'],$this->config['privateKeyFile'],$this->config['certpwd'],$this->url);

        $r=$tools->send( $params);

        if($r==FALSE)
        {
            $d['msg']=$tools->msg;
        }

        if(isset($r["AIPG"]['INFO']['RET_CODE']))
        {
            $d['code']=$r["AIPG"]['INFO']['RET_CODE'];
            $d['msg']=$r["AIPG"]['INFO']['ERR_MSG'];
        }
        else
       {
            $d['msg']='出款失败';
        }
        return $d;
    }


    /*
     * 配置
     */
    private function _config($config=[])
    {
        foreach($config as $key=>$val )
        {
            if(array_key_exists($key, $this->config))
            {
                if($val!='') $this->config[$key]=$val;
            }
        }

    }
}
